<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" />

	<xsl:key name="MessagesByCategory" match="Message[(string(@Status) != 'Excluded') and (string(@Status) != 'Absent')]" use="@Category" />
	<xsl:key name="CategoriesByTypeName" match="Message[(string(@Status) != 'Excluded') and (string(@Status) != 'Absent')]" use="concat(@Category,'+',@TypeName)" />

	<xsl:template match="/">
		<xsl:element name="ViolationSummary">
			<xsl:apply-templates select="FxCopProject/FxCopReport//Messages" />
		</xsl:element>
	</xsl:template>

	<xsl:template match="Messages">
		<xsl:for-each select="Message[count(. | key('MessagesByCategory', @Category)[1]) = 1]">
			<xsl:sort select="@Category" />
			<xsl:for-each select="key('MessagesByCategory',@Category)[generate-id() = generate-id(key('CategoriesByTypeName', concat(@Category,'+',@TypeName))[1])]">
				<xsl:sort select="@TypeName" />
				<xsl:element name="Rule">
					<xsl:attribute name="categoryName">
						<xsl:value-of select="@Category" />
					</xsl:attribute>
					<xsl:attribute name="ruleName">
						<xsl:value-of select="@TypeName" />
					</xsl:attribute>
					<xsl:attribute name="violationCount">
						<xsl:value-of select="count(key('CategoriesByTypeName', concat(@Category,'+',@TypeName)))" />
					</xsl:attribute>
				</xsl:element>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>